package com.example.blogsystem.controller;

import com.example.blogsystem.model.Result;
import com.example.blogsystem.model.UserInfo;
import com.example.blogsystem.service.UserInfoService;
import com.example.blogsystem.utils.JwtUtils;
import com.example.blogsystem.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/register")
    public Result register(UserInfo userInfo) {
        if(userInfo == null
                ||!StringUtils.hasLength(userInfo.getUserName())
                ||!StringUtils.hasLength(userInfo.getPassword())
                ||!StringUtils.hasLength(userInfo.getGithubUrl())) {
            return Result.fail("注册用户失败",false);
        }
        String finalPassword = SecurityUtils.encrypt(userInfo.getPassword());
        userInfo.setPassword(finalPassword);
        Integer ret = userInfoService.registerUserInfo(userInfo);
        if(ret > 0) {
            return Result.success(true);
        }
        return Result.fail("用户注册失败",false);
    }
    @RequestMapping("/login")
    public Result login(String userName,String password) {
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            log.error("用户名或密码输入不合法");
            return Result.fail("用户名或密码输入不合法");
        }

        UserInfo userInfo = userInfoService.queryUserByName(userName);
//        if(userInfo == null || !userInfo.getPassword().equals(password)) {
//            log.error("用户名或密码错误");
//            return Result.fail("用户名或密码错误");
//        }

        if(userInfo!= null && !SecurityUtils.verify(password,userInfo.getPassword())) {
            log.error("用户名或密码错误");
            return Result.fail("用户名或密码错误");
        }
        Map<String,Object> claims = new HashMap<String, Object>();
        claims.put("id",userInfo.getId());
        claims.put("userName",userName);

        String jwt = JwtUtils.genJwt(claims);
        return Result.success(jwt);
    }

    @RequestMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {

        UserInfo userInfo = userInfoService.getUser(request);
        if(userInfo != null) {
            log.info("拿到用户信息");
            return Result.success(userInfo);
        }
        return Result.fail("未拿到用户信息","");
    }
    @RequestMapping("/getAuthorInfo")
    public Result getAuthorInfo(@RequestParam("blogId") Integer id) {

        UserInfo authorInfo = userInfoService.getAuthor(id);
        if(authorInfo != null && authorInfo.getUserName().length() > 0) {
            log.info("拿到作者信息");
            return Result.success(authorInfo);
        }
        return Result.fail("查询失败","");
    }

    @RequestMapping("count")
    public Result count() {
        Integer integer = userInfoService.countUserInfo();
        if(integer != null && integer > 0) {
            return Result.success(integer);
        }
        return Result.fail("获取失败","");
    }
}
